<template>
  <div>
    <el-page-header @back="onClose" :content="title">
    </el-page-header>
    <el-divider></el-divider>
    <el-form :model="userForm" status-icon :rules="rules" ref="userForm" label-width="100px">
      <el-form-item label="登录名" prop="loginname">
        <el-input
          v-model="userForm.loginname"
          autocomplete="off"
          maxlength="50"
          show-word-limit>
        </el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="userForm.username"
          autocomplete="off"
          maxlength="50"
          show-word-limit>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('userForm')">提交</el-button>
        <el-button @click="resetForm('userForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addUser, queryUser, modUser } from '@/api/user'
export default {
  data () {
    var validateLoginname = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('登录名不能为空'))
      }
      setTimeout(() => {
        if (value.length > 50) {
          callback(new Error('长度超过50'))
        } else {
          callback()
        }
      }, 1000)
    }
    var validateUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      }
      setTimeout(() => {
        if (value.length > 50) {
          callback(new Error('长度超过50'))
        } else {
          callback()
        }
      }, 1000)
    }
    return {
      userForm: {
        userid: null,
        loginname: '',
        username: ''
      },
      rules: {
        loginname: [
          { validator: validateLoginname, trigger: 'blur' }
        ],
        username: [
          { validator: validateUsername, trigger: 'blur' }
        ]
      },
      opType: '1',
      title: ''
    }
  },
  created: function () {
    const query = this.$route.query
    this.opType = query.opType
    this.userForm.userid = query.userid
    if (this.opType === '1') {
      this.title = '修改用户'
      queryUser({ userid: query.userid }).then(response => {
        this.userForm = response.data
      }).catch(e => {
        console.log(e)
      })
    } else {
      this.title = '新增用户'
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.opType === '0') {
            addUser(this.userForm).then(response => {
              const { msg, nResult } = response.data
              if (nResult === '0') {
                this.$alert('新增成功', '提示', {
                  confirmButtonText: '确定',
                  type: 'success',
                  callback: action => {
                    this.onClose()
                  }
                })
              } else {
                this.$alert(msg, '提示', {
                  confirmButtonText: '确定',
                  type: 'error',
                  callback: action => {
                  }
                })
              }
            }).catch(e => {
              console.log(e)
            })
          } else {
            modUser(this.userForm).then(response => {
              const { msg, nResult } = response.data
              if (nResult === '0') {
                this.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  type: 'success',
                  callback: action => {
                    this.onClose()
                  }
                })
              } else {
                this.$alert(msg, '提示', {
                  confirmButtonText: '确定',
                  type: 'error',
                  callback: action => {
                  }
                })
              }
            }).catch(e => {
              console.log(e)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    onClose () {
      this.$router.push({ path: '/user/userList' })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
